//
//  DXHomeViewController.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/5.
//  Copyright © 2015年 simon. All rights reserved.
//

//App Key：3254877270
//App Secret：4086beb3f02ce3ed8ce8ceebe5495f25

//*"https://api.weibo.com/oauth2/authorize?client_id=3254877270&redirect_uri=http://www.itheima.com/


import UIKit
import SVProgressHUD
let KAPPKEY = "3254877270"
let KAPPSECRET = "4086beb3f02ce3ed8ce8ceebe5495f25"
let KCALLBACKURL = "http://www.itheima.com/"
class DLogInViewController: DXBaseTableViewController,UIWebViewDelegate {
    
    let webView = UIWebView(frame: UIScreen.mainScreen().bounds)
    
    override func loadView() {
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        设置navigation的按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.customItem(title: "取消", imgName: nil, target: self, action: "cancelBtnClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem.customItem(title: "自动填充", imgName: nil, target: self, action: "autoFillBtnClick")
        
        webView.delegate = self
        webView.scalesPageToFit = true
        webView.scrollView.showsHorizontalScrollIndicator = false
        webView.scrollView.showsVerticalScrollIndicator = false
        
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(KAPPKEY)&redirect_uri=\(KCALLBACKURL)"
        
        let url = NSURL(string: urlString)
        let request = NSURLRequest(URL: url!)
        webView.loadRequest(request)
        
       
        
    }
    
    
    // MARK: - 取消按钮点击事件
    @objc private func cancelBtnClick(){
        SVProgressHUD.dismiss()
        
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    // MARK: - 自动填充按钮点击事件
    @objc private func autoFillBtnClick(){
//        网页中写入js代码
//        yohtr35601@163.com qw1987
//        vgfjh15964@163.com qw1987
//        xingzhong964375@163.com
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('userId').value='vgfjh15964@163.com';document.getElementById('passwd').value='qw1987';")
        
    }
    
    // MARK: -UIWebViewDelegate
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if  request.URL!.absoluteString.hasPrefix(KCALLBACKURL) {
            
//      logPrint(request.URL!.query)
        
        let code = request.URL!.query!.substringFromIndex("code=".endIndex)
//            logPrint(code)
            
//            loadUserToken(code: code)
            
         DXUserAccountViewModel.shareUserAccount.loadUserToken(code: code, loadSuccess: { () -> () in
            
           self.dismissViewControllerAnimated(false) { () -> Void in
                //                 登录请求成功回调
                NSNotificationCenter.defaultCenter().postNotificationName(KChangeRootController, object: self)
            }

            }, loadFailure: { (error) -> () in
                logPrint(error)
         })
            
            return false
        }else{
         return true
        }
//     logPrint(request.URL?.query)
       
    }
    
    // MARK: - 开始加载网页时调用的代理方法
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
       
        
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
}

